// pages/channel/home/home.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identityInfo: '', // 用户身份信息
    dataInfo: '',
    divideInfo: '', // 分成信息
    dividePopShow: false, // 我的分成弹窗
    channelcodePopShow: false, // 渠道码弹窗
    channel_adImg: app.globalData._network_path + 'channel_ad.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIdentityInfo();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  // 获取用户身份信息
  getIdentityInfo() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/getUserIdentityInfo',
      data: {
        user_id: user_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            identityInfo: res.data.data
          })
          // wx.setStorageSync('identity',res.data.data.identity);
          that.getInfo(res.data.data.identity);
          that.getDivideInfo();
        }
      }
    })
  },
  // 获取个人信息
  getInfo(identity) {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var _url = '';
    if(identity == 2){ // 渠道商
      _url = 'User/Channel/getChannelInfo';
    }else if(identity == 3){ // 推广商
      _url = 'User/Channel/getPromotersInfo';
    }
    app.ajax({
      url: _url,
      data: {
        user_id: user_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            dataInfo: res.data.data
          })
        }
      }
    })
  },
  // 获取分成比例列表
  getDivideInfo() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/getChannelCommissionList',
      data: {
        user_id: user_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            divideInfo: res.data.data.data
          })
        }
      }
    })
  },
  // 跳转红包明细
  goRedpacketDetail() {
    wx.navigateTo({
      url: '/pages/channel/redpacketDetail/redpacketDetail',
    })
  },
  // 提现
  cashWithdrawal() {
    wx.navigateTo({
      // url: '/pages/subpackage/pages/channel/accounputForward/accounputForward',
      url: '/pages/user/accounputForward/accounputForward?role=4',
    })
  },
  // 跳转团队管理
  goTeamManage() {
    wx.navigateTo({
      url: '/pages/channel/teamManage/teamManage',
    })
  },
  // 我的提现
  presentRecord(){
    var that = this;
    wx.navigateTo({
      url: '/pages/subpackage/pages/channel/presentRecord/presentRecord',
    })
  },
  // 跳转邀请记录
  goInvite() {
    wx.navigateTo({
      url: '/pages/user/myInvite/myInvite',
    })
  },
  // 我的分成
  myDivide() {
    this.setData({
      dividePopShow: true
    })
  },
  // 关闭分成弹窗
  coloseDividePop(){
    this.setData({
      dividePopShow: false
    })
  },
  // 渠道码
  channelCode(){
    this.setData({
      channelcodePopShow: true
    })
  },
  // 关闭渠道码弹窗
  coloseChannelcodePop() {
    this.setData({
      channelcodePopShow: false
    })
  },
  // 保存渠道码
  saveCode() {
    var that = this;
    wx.showLoading({
      title: '保存中...'
    })
    wx.downloadFile({
      url: this.data.dataInfo.qrcode, 
      success (res) {
        console.log(123,res)
        if (res.statusCode === 200) {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(data) {
              wx.hideLoading();
              app.showToast('保存成功');
              that.setData({
                channelcodePopShow: false
              })
            },
            fail: function (err) {
              if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
                wx.showModal({
                  title: '提示',
                  content: '需要您授权保存相册',
                  showCancel: false,
                  success:modalSuccess=>{
                    wx.openSetting({
                      success(settingdata) {
                        console.log("settingdata", settingdata)
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                          wx.showModal({
                            title: '提示',
                            content: '获取权限成功,再次点击图片即可保存',
                            showCancel: false,
                          })
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '获取权限失败，将无法保存到相册哦~',
                            showCancel: false,
                          })
                        }
                      },
                      fail(failData) {
                        console.log("failData",failData)
                      },
                      complete(finishData) {
                        console.log("finishData", finishData)
                      }
                    })
                  }
                })
              }
            },
            complete(res) {
              wx.hideLoading()
            }
          })
        }
      }
    })
    
  },
  // 邀请好友
  invitation() {
    wx.navigateTo({
      // url: '/pages/user/inviteFriends/inviteFriends',
      url: '/pages/marketing/marketing',
    })
  },
})